<?php

namespace common\models\member;

use Yii;

/**
 * This is the model class for table "{{%member_data}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $member_id 会员ID
 * @property string $realname 真实姓名
 * @property string $mobile 手机号
 * @property string $idcardno 身份证号
 * @property string $photo 人像照
 * @property string $passport_front 证件正面
 * @property string $passport_reverse 证件反面
 * @property int $status 资料状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property string $mark 备注
 */
class MemberData extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\relation\User;

    CONST STATUS_ACTIVE = 10;
    CONST STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_data}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'member_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['mobile'], 'required'],
            [['realname'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
            [['idcardno'], 'string', 'max' => 18],
            [['photo', 'passport_front', 'passport_reverse', 'mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'member_id' => '会员ID',
            'realname' => '真实姓名',
            'mobile' => '手机号',
            'idcardno' => '身份证号',
            'photo' => '人像照',
            'passport_front' => '证件正面',
            'passport_reverse' => '证件反面',
            'status' => '资料状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'mark' => '备注',
        ];
    }

    /**
     * 关联会员
     */
    public function getMember() {
        return $this->hasOne(Member::className(), ['id' => 'member_id']);
    }

    /**
     * 关联会员
     */
    public function getType() {
        return $this->hasOne(MemberType::className(), ['id' => 'type_id'])->via('member');
    }

    /**
     * 关联会员个人资料信息
     */
    public function getPersonal() {
        return $this->hasOne(MemberDataPersonal::className(), ['member_id' => 'member_id']);
    }

    /**
     * 关联会员个人资料信息
     */
    public function getAccount() {
        return $this->hasOne(MemberAccount::className(), ['member_id' => 'member_id']);
    }

    /**
     * 关联会员
     */
    public function getBranch() {
        return $this->hasMany(\common\models\user\UserBranch::className(), ['member_id' => 'member_id']);
    }

    /**
     * 关键字检索会员信息
     */
    public static function findByKeyword($keyword) {
        $query = static::find()->alias('t')->active();
        $query->innerJoinWith([
            'member member' => function ($q) {
                $q->active(Member::STATUS_ACTIVE)->andWhere([
                    'member.audit_status' => Member::AUDIT_STATUS_FINISH
                ]);
            }
        ]);
        $query->andFilterWhere([
            'or',
            ['t.user_id' => $keyword],
            ['t.member_id' => $keyword],
            ['t.realname' => $keyword],
            ['t.idcardno' => $keyword],
            ['t.mobile' => $keyword],
        ]);
        return $query->all();
    }

}
